<?php
declare (strict_types=1);

namespace app\api\controller;

use app\common\model\Bank;
use app\common\model\BusinessApply as BusinessApplyModel;
use app\common\tool\Result;
use think\exception\ValidateException;
use think\Response;

class BusinessApply extends Base
{

    /**
     * @return Response
     * 原点 2022年12月22日10:54:56 申请前置
     */
    public function index(): Response
    {
        try {
            $info = BusinessApplyModel::where('uid', $this->user->id)
                ->with(['p', 'c', 'd'])
                ->withoutField('uid,full_address,create_time,update_time,is_del,admin_id,admin_name,audit_time')
                ->order('create_time', 'desc')
                ->find();
            $result = [
                'status'             => $info ? $info->getData('status') : 0,
                'applyData'          => $info,
                'business_agreement' => handleRichText($this->system['business_agreement']),
                'bank'               => Bank::where(['is_del' => 0, 'status' => 1])->field('id,title')->select()
            ];
            return Result::success('success', $result);
        } catch (\Exception $e) {
            return Result::fail('加载失败，服务器错误！');
        }
    }

    /**
     * @return Response
     * 原点 2022年12月22日10:52:54 申请商家
     */
    public function add(): Response
    {
        try {
            //if($this->user->syn_level < 3) return Result::fail('会员级别不符，无权申请商家！');
            $param = $this->request->post();
            $this->validate($param, 'BusinessApply');
            return Result::fastReturn(BusinessApplyModel::add($param, $this->user));
        } catch (ValidateException $e) {
            return Result::fail($e->getMessage());
        } catch (\Exception $e) {
            return Result::fail('加载失败，服务器错误！');
        }
    }
}

